using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Jbe.TestSuite.TestDevice.FunctionGen.Interface.Services;
using System.Diagnostics;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Views
{
    public partial class ConfigurationView : UserControl
    {
        public event EventHandler IsValidChanged;

        private IFunctionGenConfig config;
        private bool isValid;
        private bool readOnly;
        private bool suspendWaveFormGroupCheckedChanged;
        private RadioButton[] waveFormGroup;

        [Browsable(false)]
        public IFunctionGenConfig Config
        {
            get { return config; }
            set 
            { 
                config = value;
                functionGenConfigBindingSource.DataSource = value;
                UpdateWaveFormGroup();
            }
        }

        [Browsable(false)]
        public bool IsValid
        {
            get { return isValid; }
            private set 
            {
                if (isValid != value)
                {
                    isValid = value;
                    OnIsValidChanged();
                }
            }
        }

        [DefaultValue(true)]
        public bool ReadOnly
        {
            get { return readOnly; }
            set 
            { 
                readOnly = value;
                sineRadio.Enabled = !value;
                squareRadio.Enabled = !value;
                triangleRadio.Enabled = !value;
                sawtoothRadio.Enabled = !value;
                amplitudeText.ReadOnly = value;
                frequencyText.ReadOnly = value;
            }
        }

        public ConfigurationView()
        {
            InitializeComponent();
            
            suspendWaveFormGroupCheckedChanged = false;
            InitializeWaveFormGroup();

            ReadOnly = true;
        }

        protected virtual void OnIsValidChanged()
        {
            if (IsValidChanged != null) { IsValidChanged(this, new EventArgs()); }
        }

        private void InitializeWaveFormGroup() 
        {
            waveFormGroup = new RadioButton[4];
            waveFormGroup[0] = sineRadio;
            waveFormGroup[1] = squareRadio;
            waveFormGroup[2] = triangleRadio;
            waveFormGroup[3] = sawtoothRadio;

            sineRadio.Tag = WaveForm.Sine;
            squareRadio.Tag = WaveForm.Square;
            triangleRadio.Tag = WaveForm.Triangle;
            sawtoothRadio.Tag = WaveForm.Sawtooth;

            foreach (RadioButton button in waveFormGroup)
            {
                button.CheckedChanged += new EventHandler(WaveFormGroupCheckedChanged);
            }
        }

        private void UpdateWaveFormGroup()
        {
            suspendWaveFormGroupCheckedChanged = true;
            foreach (RadioButton button in waveFormGroup)
            {
                button.Checked = (config.WaveForm == (WaveForm)button.Tag);
            }
            suspendWaveFormGroupCheckedChanged = false;
        }
        
        private void WaveFormGroupCheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked && !suspendWaveFormGroupCheckedChanged)
            {
                config.WaveForm = (WaveForm)((RadioButton)sender).Tag;
            }
        }

        private void FunctionGenConfigBindingSourceBindingComplete(object sender, BindingCompleteEventArgs e)
        {
            IsValid = ValidateChildren();
        }
    }
}
